package org.lfh.wintersun.web.secondKill.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.lfh.wintersun.common.base.PageUtils;
import org.lfh.wintersun.web.secondKill.dao.SecondKillVoucherDao;
import org.lfh.wintersun.web.secondKill.entity.SecondKillVoucherEntity;
import org.lfh.wintersun.web.secondKill.entity.SecondKillVoucherExtEntity;
import org.lfh.wintersun.web.secondKill.service.SecondKillVoucherExtService;
import org.lfh.wintersun.web.secondKill.service.SecondKillVoucherService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * (SecondKillVoucher)表服务实现类
 *
 * @author lfh
 * @since 2025-04-12 23:04:15
 */
@Service("secondKillVoucherService")
public class SecondKillVoucherServiceImpl extends ServiceImpl<SecondKillVoucherDao, SecondKillVoucherEntity>
        implements SecondKillVoucherService {

    @Resource
    private SecondKillVoucherExtService secondKillVoucherExtService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SecondKillVoucherEntity queryById(Long id) {
        SecondKillVoucherEntity voucherEntity = this.getById(id);
        if (voucherEntity != null) {
            SecondKillVoucherExtEntity secondKillVoucherExtEntity = secondKillVoucherExtService.getOneByVoucherId(id);
            voucherEntity.setSecondKillVoucherExtEntity(secondKillVoucherExtEntity);
        }
        return voucherEntity;

    }

    /**
     * 分页查询
     *
     * @param pageNum 页码
     * @param size    每页数量
     * @return 分页结果
     */
    @Override
    public PageUtils<SecondKillVoucherEntity> queryByPage(Integer pageNum, Integer size) {
        IPage<SecondKillVoucherEntity> iPage =
                this.baseMapper.queryAllByLimit(new Page<>(pageNum, size));
        setSecondKillVoucherExtEntity(iPage);
        return new PageUtils<>(iPage);
    }

    private void setSecondKillVoucherExtEntity(IPage<SecondKillVoucherEntity> iPage) {
        List<SecondKillVoucherEntity> records = iPage.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            List<Long> ids = records.stream().map(SecondKillVoucherEntity::getId).toList();
            List<SecondKillVoucherExtEntity> secondKillVoucherExtEntities = secondKillVoucherExtService.listByVoucherIds(ids);
            if (CollUtil.isNotEmpty(secondKillVoucherExtEntities)) {
                records.forEach(voucherEntity -> {
                    secondKillVoucherExtEntities.stream()
                            .filter(e -> Objects.equals(e.getVoucherId(), voucherEntity.getId()))
                            .findFirst()
                            .ifPresent(voucherEntity::setSecondKillVoucherExtEntity);
                });
            }
        }
    }

    /**
     * 新增数据
     *
     * @param secondKillVoucherEntity 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SecondKillVoucherEntity insert(SecondKillVoucherEntity secondKillVoucherEntity) {
        this.save(secondKillVoucherEntity);
        SecondKillVoucherExtEntity secondKillVoucherExtEntity = secondKillVoucherEntity.getSecondKillVoucherExtEntity();
        secondKillVoucherExtEntity.setVoucherId(secondKillVoucherExtEntity.getVoucherId());
        secondKillVoucherExtEntity.setVoucherId(secondKillVoucherEntity.getId());
        secondKillVoucherExtService.insert(secondKillVoucherExtEntity);
        return secondKillVoucherEntity;
    }

    /**
     * 修改数据
     *
     * @param secondKillVoucherEntity 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SecondKillVoucherEntity update(SecondKillVoucherEntity secondKillVoucherEntity) {
        this.updateById(secondKillVoucherEntity);
        SecondKillVoucherExtEntity secondKillVoucherExtEntity = secondKillVoucherEntity.getSecondKillVoucherExtEntity();
        secondKillVoucherExtService.update(secondKillVoucherExtEntity);
        return secondKillVoucherEntity;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    public void deleteById(Long id) {
        secondKillVoucherExtService.deleteById(id);
        this.removeById(id);
    }

    /**
     * 通过主键集合删除数据
     *
     * @param ids 主键集合
     */
    @Override
    public void deleteIds(List<Long> ids) {
        super.removeByIds(ids);
    }
}
